package jk.spider.core.task.work.impl;

import jk.spider.core.SpiderController;
import jk.spider.core.event.impl.ResourceParsedOkEvent;
import jk.spider.core.event.impl.URLFoundEvent;
import jk.spider.core.task.work.InterpreteHTMLTask;
import jk.spider.model.Resource;
import jk.spider.util.SpiderUtil;

public class DefaultInterpreteHTMLTask extends InterpreteHTMLTask {

	public DefaultInterpreteHTMLTask(SpiderController controller,
			Resource resource) {
		super(controller, resource);
	}
	
	public void execute() {
		SpiderUtil util = new SpiderUtil();
		String htmlSource = this.getHtmlSource();
		
		String [] links = util.findAllUrl(htmlSource.toString(),resource.getUrl());
		this.urlFound(links);
		links = null;
		notifyEvent(resource, new ResourceParsedOkEvent(controller, resource) );
			
	}
	
	private void urlFound(String [] links) {
		if(links != null && links.length > 0) {
			for(String url : links) {
				notifyEvent(resource, new URLFoundEvent(controller, resource, url));
			}
		}
	}

	public void prepare() {
		
	}

}
